<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>百聆 - 智能语音助手</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
        }
        .chat-box {
            height: 500px;
            overflow-y: auto;
            border: 1px solid #eee;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
        }
        .message {
            margin-bottom: 15px;
            display: flex;
            align-items: flex-start;
        }
        .message.user {
            flex-direction: row-reverse;
        }
        .message-content {
            max-width: 70%;
            padding: 10px 15px;
            border-radius: 15px;
            margin: 0 10px;
        }
        .user .message-content {
            background-color: #007AFF;
            color: white;
        }
        .assistant .message-content {
            background-color: #E9ECEF;
            color: black;
        }
        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: #ddd;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
        }
        .user .avatar {
            background-color: #007AFF;
            color: white;
        }
        .assistant .avatar {
            background-color: #28a745;
            color: white;
        }
        .input-area {
            display: flex;
            gap: 10px;
        }
        input[type="text"] {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        button {
            padding: 10px 20px;
            background-color: #007AFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .time {
            font-size: 12px;
            color: #888;
            margin: 5px 0;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 style="text-align: center;">百聆 - 智能语音助手</h1>
        <div class="chat-box" id="chatBox"></div>
        <div class="input-area">
            <input type="text" id="messageInput" placeholder="输入消息..." onkeypress="handleKeyPress(event)">
            <button onclick="sendMessage()">发送</button>
        </div>
    </div>

    <script>
        let ws = new WebSocket(`ws://${window.location.host}/ws`);
        const chatBox = document.getElementById('chatBox');
        const messageInput = document.getElementById('messageInput');

        ws.onmessage = function(event) {
            const message = JSON.parse(event.data);
            appendMessage(message);
        };

        ws.onclose = function(event) {
            console.log("WebSocket连接已关闭");
            appendSystemMessage("连接已断开，请刷新页面重新连接");
        };

        function handleKeyPress(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        }

        function sendMessage() {
            const content = messageInput.value.trim();
            if (content) {
                const message = {
                    role: 'user',
                    content: content,
                    time: new Date().toLocaleString()
                };
                ws.send(JSON.stringify(message));
                messageInput.value = '';
            }
        }

        function appendMessage(message) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${message.role}`;
            
            const avatar = document.createElement('div');
            avatar.className = 'avatar';
            avatar.textContent = message.role === 'user' ? '我' : '助';

            const contentDiv = document.createElement('div');
            contentDiv.className = 'message-content';
            contentDiv.textContent = message.content;

            const timeDiv = document.createElement('div');
            timeDiv.className = 'time';
            timeDiv.textContent = message.time || new Date().toLocaleString();

            if (message.role === 'user') {
                messageDiv.appendChild(contentDiv);
                messageDiv.appendChild(avatar);
            } else {
                messageDiv.appendChild(avatar);
                messageDiv.appendChild(contentDiv);
            }
            
            chatBox.appendChild(messageDiv);
            chatBox.appendChild(timeDiv);
            chatBox.scrollTop = chatBox.scrollHeight;
        }

        function appendSystemMessage(content) {
            const message = {
                role: 'system',
                content: content,
                time: new Date().toLocaleString()
            };
            appendMessage(message);
        }
    </script>
</body>
</html> 